﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CuaHangBangDia.QLHoaDon
{
    public partial class ThongKeHoaDonForm : Form
    {
        public ThongKeHoaDonForm()
        {
            InitializeComponent();
        }

        private void FormThongKeHoaDon_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSetQLCuaHangBangDia.View_ThongKeHoaDon' table. You can move, or remove it, as needed.
           // this.view_ThongKeHoaDonTableAdapter.Fill(this.dataSetQLCuaHangBangDia.View_ThongKeHoaDon);
            lblNgayLap.Visible = false;
            dtpTKHD.Visible = false;
            lblMHD.Visible = false;
            mtbMHD.Visible = false;

        }

        private void btnThongKeHD_Click(object sender, EventArgs e)
        {
            if (rdMHD.Checked == false && rdTKHDNam.Checked == false && rdTKHDNgay.Checked == false && rdTKHDThang.Checked == false)
            {
                MessageBox.Show("Vui lòng chọn kiểu thống kê!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                DateTime dt = dtpTKHD.Value;
                int ngay = dt.Day;
                decimal thang = dt.Month;
                decimal nam = dt.Year;
                if (rdMHD.Checked == true)
                {
                    if (mtbMHD.Text.Remove(0, 2) != "")
                    {
                        
                        int mahd = Convert.ToInt32(mtbMHD.Text.Remove(0, 2));
                        List<HoaDon> ls = HoaDon.Objects.Find(i => i.MaHoaDon == mahd).ToList();
                        if (ls.Count > 0)
                        {
                            this.view_ThongKeHoaDonTableAdapter.Fill(this.dataSetQLCuaHangBangDia.View_ThongKeHoaDon, 0, 0, 0, mahd);
                            this.reportViewer1.RefreshReport();
                        }
                        else
                        {
                            MessageBox.Show("Mã hóa đơn không tồn tại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Nhập mã hóa đơn cần thống kê!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                if (rdTKHDNgay.Checked == true)
                {
                    this.view_ThongKeHoaDonTableAdapter.Fill(this.dataSetQLCuaHangBangDia.View_ThongKeHoaDon, ngay, thang, nam,0);
                    this.reportViewer1.RefreshReport();
                }
                if (rdTKHDThang.Checked == true)
                {
                    this.view_ThongKeHoaDonTableAdapter.Fill(this.dataSetQLCuaHangBangDia.View_ThongKeHoaDon, 0, thang, nam,0);
                    this.reportViewer1.RefreshReport();
                }
                if (rdTKHDNam.Checked == true)
                {
                    this.view_ThongKeHoaDonTableAdapter.Fill(this.dataSetQLCuaHangBangDia.View_ThongKeHoaDon, 0, 0, nam,0);
                    this.reportViewer1.RefreshReport();
                }



            }
        }

        private void txtMHD_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {

                e.Handled = true;

            }

        }

        private void rdTKHDNgay_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayLap.Visible = true;
            dtpTKHD.Visible = true;
            lblMHD.Visible = false;
            mtbMHD.Visible = false;
        }

        private void rdTKHDThang_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayLap.Visible = true;
            dtpTKHD.Visible = true;
            lblMHD.Visible = false;
            mtbMHD.Visible = false;
        }

        private void rdTKHDNam_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayLap.Visible = true;
            dtpTKHD.Visible = true;
            lblMHD.Visible = false;
            mtbMHD.Visible = false;
        }

        private void rdMHD_CheckedChanged(object sender, EventArgs e)
        {
            lblNgayLap.Visible = false;
            dtpTKHD.Visible = false;
            lblMHD.Visible = true;
            mtbMHD.Visible = true;
        }

        //private void txtMHD_TextChanged(object sender, EventArgs e)
        //{

        //    if (Convert.ToInt32(txtMHD.Text) > 32767)
        //    {
        //        MessageBox.Show("Chỉ được nhập số nhỏ hơn 32767!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        txtMHD.Text = "0";
        //    }



        //}


    }
}
